package com.yong.sbd.biz.mybatis.controller;

import com.yong.sbd.biz.mybatis.entity.SysUser;
import com.yong.sbd.biz.mybatis.service.UserRoleService;
import com.yong.sbd.common.beans.response.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户角色控制器
 *
 * @author Kang Yong
 * @date 2025/10/24
 * @since 1.0.0
 */
@RestController
@RequestMapping("/user-role")
public class UserRoleController {

    @Autowired
    private UserRoleService userRoleService;

    /**
     * 根据用户ID查询用户及其角色信息
     *
     * @param userId {@link Long}
     * @return {@link R<SysUser>}
     */
    @GetMapping("/user/{userId}")
    public R<SysUser> getUserWithRoles(@PathVariable Long userId) {
        return R.success(userRoleService.getUserWithRoles(userId));
    }

    /**
     * 查询所有用户及其角色信息
     *
     * @return {@link R<List<SysUser>>}
     */
    @GetMapping("/users")
    public R<List<SysUser>> getAllUsersWithRoles() {
        return R.success(userRoleService.getAllUsersWithRoles());
    }

    /**
     * 根据角色ID查询用户列表
     *
     * @param roleId {@link Long}
     * @return {@link R<List<SysUser>>}
     */
    @GetMapping("/role/{roleId}/users")
    public R<List<SysUser>> getUsersByRoleId(@PathVariable Long roleId) {
        return R.success(userRoleService.getUsersByRoleId(roleId));
    }

    /**
     * 根据角色编码查询用户列表
     *
     * @param roleCode {@link String}
     * @return {@link R<List<SysUser>>}
     */
    @GetMapping("/role-code/{roleCode}/users")
    public R<List<SysUser>> getUsersByRoleCode(@PathVariable String roleCode) {
        return R.success(userRoleService.getUsersByRoleCode(roleCode));
    }
}